package com.vecspace.dayu.code.template.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.vecspace.dayu.code.consts.TerminalType;
import org.springframework.stereotype.Repository;

import com.vecspace.database.dao.BaseDao;
import com.vecspace.dayu.code.template.pojo.CodeTemplate;

/** DAYU_CODE_CODE_TEMPLATE,操作数据库的dao类
 * @author: wanghua
 */
@Repository("dayu_codeTemplateDao")
public class CodeTemplateDao extends BaseDao {
	/** 根据ID查询 DAYU_CODE_CODE_TEMPLATE
	 * @param id   主键值
	 * @return CodeTemplate
	 * @throws Exception
	 * @author wanghua
	 */
	public CodeTemplate getCodeTemplate(Long id) throws Exception {
		CodeTemplate codeTemplate = null;
		if (id != null && id != 0) {

			codeTemplate = this.queryObject(CodeTemplate.class, id);
			this.evict(codeTemplate);
		}
		return codeTemplate;
	}
	
	/**获取一个布局类型下的所有页面模板
	 * 
	 * @param layoutId
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public List<CodeTemplate> getCodeTemplatesByLayout(Long layoutId) throws Exception{
		List<CodeTemplate> templates = this.queryObjectList("from CodeTemplate where layoutId=?0 order by ordnum", new Object[] {layoutId});
		return templates;
	}
	
	public int getCodeTemplatesCountByLayout(Long layoutId) throws Exception{
		int count = this.queryInt("select count(*) from DAYU_CODE_TEMPLATE where layout_id=?", new Object[] {layoutId});
		return count;
	}
	
	/**获取布局下包含的源代码文件类型
	 * 
	 * @param pagelayoutId
	 * @return
	 * @throws Exception 
	 * @author Administrator
	 * @reviewer  
	 * @createTime 2021年9月22日 上午10:16:36
	 */
	public List<String> getCodeTypes(Long pagelayoutId) throws Exception{
		String sql = "select distinct CODE_TYPE from dayu_code_template t1 where LAYOUT_ID=?";
		List<Object[]> rows = this.queryDataRowList(sql, new Object[] {pagelayoutId});
		List<String> codeTypes = new ArrayList<>();
		for(Object[] row : rows) {
			if(row[0] == null)continue;
			codeTypes.add(row[0].toString());
		}
		return codeTypes;
	}

	/**获取指定布局以及通用终端布局下代码类型为codetype的模板
	 *
	 * @param pageLayoutId
	 * @param codeType
	 * @return
	 */
	public List<CodeTemplate> getCodeTemplatesByCodeTypeWithCommon(Long pageLayoutId, String codeType) throws Exception {
		String sql0 = "SELECT ID FROM DAYU_CODE_TEMPLATE_PAGE_LAYOUT WHERE TERMINAL_TYPE=?";
		String sql = "select * from DAYU_CODE_TEMPLATE where (LAYOUT_ID=? or LAYOUT_ID in ("+sql0+")) and CODE_TYPE=? order by ordnum";
		List<Map<String, Object>> rows = this.queryDataRowMapList(sql, new Object[]{pageLayoutId, TerminalType.COMMON, codeType});
		List<CodeTemplate> items = new ArrayList<>();
		for(Map<String, Object> row : rows){
			CodeTemplate item = new CodeTemplate();
			item.parseFrom(row);
			items.add(item);
		}
		return items;
	}
	
	/** 插入或修改 DAYU_CODE_CODE_TEMPLATE 
	 * @param item	要保存的对象
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	public void saveCodeTemplate(CodeTemplate item) throws Exception {
		boolean addFlag = item.getId() == null;
		this.saveOrUpdateObject(item);
		this.evict(item);
		if(addFlag){
			this.onAddDataRow("DAYU_CODE_CODE_TEMPLATE", item);
		}else{
			this.onUpdateDataRow("DAYU_CODE_CODE_TEMPLATE", item);
		}
	}

	/** 根据ID删除 DAYU_CODE_CODE_TEMPLATE 
	 * @param id	主键值
	 * @return void
	 * @throws Exception
	 * @author wanghua
	 */
	public void deleteCodeTemplate(Long id) throws Exception {
		if (id != null && id != 0) {
			this.deleteObject(CodeTemplate.class, id);
			this.onDeleteDataRow("DAYU_CODE_CODE_TEMPLATE",id);
		}
	}
}
